package com.bootdo.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bootdo.api.domain.Message;
import com.bootdo.api.domain.OfflineBody;
import com.bootdo.api.domain.OfflineHead;
import com.bootdo.api.domain.Response;
import com.bootdo.api.domain.Status;
import com.bootdo.api.textUtil.Encipherment;
import com.bootdo.api.upload.RealNameTask;
import com.bootdo.api.util.AnnotationValidateUtils;
import com.bootdo.api.util.ExceptionType;
import com.bootdo.api.util.HttpClientClassMap;
import com.bootdo.api.util.IOUtils;
import com.bootdo.api.util.InterfaceBean;
import com.bootdo.api.util.JsonPluginsUtil;
import com.bootdo.api.util.JsonValidator;
import com.bootdo.api.util.StringHelper;
import com.bootdo.api.util.StringUtil;
import com.bootdo.api.util.ValidateException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 接口总入口
 * 
 * @author lzr
 *
 */
@RestController
@RequestMapping("/Platform")
public class HttpClientFilter extends HttpServlet {

	private static final Logger log = LoggerFactory.getLogger(HttpClientFilter.class);
	
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@RequestMapping("/HttpClientFilter")
	public String doPost(String ciphertext, ServletRequest request, ServletResponse response) throws Exception {
		// 创建加、解密类对象
		Encipherment encipherment = new Encipherment();
		OfflineBody body = new OfflineBody();
		OfflineHead head = new OfflineHead();
		String obvious = "";

		HttpServletResponse httpResponse = (HttpServletResponse) response;
		httpResponse.addHeader("Access-Control-Allow-Origin", "*");
		// 接口请求开始时间
		Date beginTime = new Date();
		long beginMilliseconds = beginTime.getTime();
		String number = String.valueOf(beginMilliseconds);
		log.info(beginTime + "--------" + beginMilliseconds);
		response.setCharacterEncoding("UTF-8");

		// 获取在请求头里Authorization的key
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		if ("POST".equals(httpRequest.getMethod())) {
			String key = httpRequest.getHeader("Authorization");
			if (key == "" || key == null) {
				Response res = new Response(Status.ERROR.toString(), Message.E005.msg(), null, null);
				JSONObject json = JSONObject.fromObject(res);
				log.info("response自行车:" + json.toString());
				return json.toString();
			}
			if (ciphertext == "" || ciphertext == null) {
				Response res = new Response(Status.ERROR.toString(), Message.E006.msg(), null, null);
				JSONObject json = JSONObject.fromObject(res);
				log.info("response自行车:" + json.toString());
				return json.toString();
			}
			obvious = encipherment.Decrypt(ciphertext, key.getBytes("utf-8"));
			log.info("解密：" + obvious);
			if (obvious == "" || obvious == null) {
				Response res = new Response(Status.ERROR.toString(), Message.E004.msg(), null, null);
				JSONObject json = JSONObject.fromObject(res);
				log.info("response自行车:" + json.toString());
				return json.toString();
			}
			JSONObject object = JSONObject.fromObject(obvious);
			log.info("接收自行车数据:" + object.toString());
			Object TermID = object.get("TermID"); // 终端编号
			Object TermSeq = object.get("TermSeq"); // 终端流水号
			Object SamID = object.get("SamID"); // PSAM卡号
			Object SamSeq = object.get("SamSeq"); // PSAM流水号
			Object Amount = object.get("Amount");//对接钱包的消费金额
			Object OrderId = object.get("OrderId");//提供给省卡的TermSeq字段
			Object Resource = object.get("Resource");//自行车方提供的钱包响应密文数据
			//JSONObject resource = JSONObject.fromObject(object.getString("Resource"));
			log.info("自行车方提供的钱包响应密文数据:" + Resource);

			// 判断三个必填参数是否为空
			if (TermID.equals("") || TermSeq.equals("") || Amount.equals("")) {
				Response res = new Response(Status.ERROR.toString(), Message.E001.msg(), null, null);
				JSONObject json = JSONObject.fromObject(res);
				log.info("response自行车:" + json.toString());
				return json.toString();
			}
			
			body.setTermID(TermID.toString());
			body.setTermSeq(TermSeq.toString());

			if (SamID.equals(""))
				body.setSamID(number);
			else
				body.setSamID(SamID.toString());
			if (SamSeq.equals(""))
				body.setSamSeq(RealNameTask.frontZero(number,8));
			else
				body.setSamSeq(RealNameTask.frontZero(SamSeq.toString(),8));
			Response res = new Response(Status.SUCCESS.toString(), Message.E002.msg(), null, null);
			JSONObject json = JSONObject.fromObject(res);

			RealNameTask real = new RealNameTask();
			int a = real.upload(body, head, Amount.toString(), OrderId.toString(), Resource.toString());
			if(a == 1){
				Response res1 = new Response(Status.ERROR.toString(), Message.E007.msg(), null, null);
				json = JSONObject.fromObject(res1);
				log.info("response自行车:" + json.toString());
				return json.toString();
			}else if(a == 2){
				Response res1 = new Response(Status.SUCCESS.toString(), Message.E008.msg(), null, null);
				json = JSONObject.fromObject(res1);
				log.info("response自行车:" + json.toString());
				return json.toString();
			}
			log.info("response自行车:" + json.toString());
			return json.toString();
		}
		return "请使用POST请求方式";
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
}